/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.CriteriaQuery;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.EventOperationTypeEnum;
import com.legendshop.oa.constants.EventTypeEnum;
import com.legendshop.oa.dao.EventDao;
import com.legendshop.oa.dto.EventQueryDto;
import com.legendshop.oa.model.UserEvent;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.util.DateUtil;

import java.util.Date;

/**
 * 记录事件.
 */

public class EventDaoImpl extends GenericDaoImpl<UserEvent, Long> implements EventDao {

	public UserEvent getEvent(Long id) {
		return getById(id);
	}

	public Long saveEvent(UserEvent userEvent) {
		return (Long) save(userEvent);
	}

	@Override
	public PageSupport<UserEvent> getEventPage(EventQueryDto eventQueryDto) {
		CriteriaQuery cq = new CriteriaQuery(UserEvent.class, eventQueryDto.getCurPageNO());
		cq.setPageSize(eventQueryDto.getPageSize());
		cq.eq("type", eventQueryDto.getType());
		cq.eq("userName",  eventQueryDto.getUserName());
		cq.eq("relateId", eventQueryDto.getRelateId());
		cq.addOrder("desc", "createTime");
		return queryPage(cq);
	}

	@Override
	public Long saveEvent(UserDetail ud, Long relateId,String relateData, EventTypeEnum eventType,EventOperationTypeEnum eventOperationType) {
		String title = "，由<b>" + ud.getRealName() + "</b>" + eventOperationType.value() + "。";
		return saveEvent(ud, relateId, title,relateData, eventType, eventOperationType);
	}
	
	@Override
	public Long saveEvent(UserDetail ud, Long relateId,String title, String relateData, EventTypeEnum eventType,EventOperationTypeEnum eventOperationType) {
		Date date = new Date();
		UserEvent userEvent = new UserEvent();
		userEvent.setCreateTime(date);
		userEvent.setOperation(eventOperationType.name());
		userEvent.setRelateId(relateId);
		String title1 = DateUtil.DateToString(date, DateUtil.CM_LONG_DATE_FORMAT) + title;
		userEvent.setTitle(title1);
		userEvent.setRelateData(relateData);
		userEvent.setType(eventType.value());
		userEvent.setUserName(ud.getUsername());
		userEvent.setRealName(ud.getRealName());
		return (Long) save(userEvent); 
	}

}
